5 inline double dist(const point
&a
, const point
&b
){
6 return sqrt((a
.x
-b
.x
)*(a
.x
-b
.x
) + (a
.y
-b
.y
)*(a
.y
-b
.y
));
9 inline double distsqr(const point
&a
, const point
&b
){
10 return (a
.x
-b
.x
)*(a
.x
-b
.x
) + (a
.y
-b
.y
)*(a
.y
-b
.y
);
14 Returns the closest distance between point pnt and the segment that goes from point a to b
15 Idea by: http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/
17 double distance_point_to_segment(const point
&a
, const point
&b
, const point
&pnt
){
18 double u
= ((pnt
.x
- a
.x
)*(b
.x
- a
.x
) + (pnt
.y
- a
.y
)*(b
.y
- a
.y
)) / distsqr(a
, b
);
20 intersection
.x
= a
.x
+ u
*(b
.x
- a
.x
);
21 intersection
.y
= a
.y
+ u
*(b
.y
- a
.y
);
22 if (u
< 0.0 || u
> 1.0){
23 return min(dist(a
, pnt
), dist(b
, pnt
));
25 return dist(pnt
, intersection
);